﻿Imports System.IO

Public Class NhanVien
    Private status As Boolean = False
    Private dsdt As ArrayList ' ds dan toc
    Private dstg As ArrayList ' ds ton giao
    Private dsbc As ArrayList ' ds bang cap
    Private dsqt As ArrayList 'ds quoc tich
    Private dscv As ArrayList 'ds chuc vu
    Private data As DataTable
    Private dlieu As String = ""
    Private filePatch As String 'chua duong dan den thu muc hinh da duyet
    Private maph As String = ""
    Private xlnv As XLNhanVien
    Dim nodeClick As String = ""
    Dim vt As Integer = 0
    Private dsnv As New List(Of DTO_NhanVienCT.NhanVien)
    Private dspb As List(Of DTO_PhongBan.PhongBan)
    Private MyImage As Bitmap

    Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
        xlnv = New XLNhanVien
        If xlnv.BUS_SaveNV(data) = True Then
            MessageBox.Show("Lưu Thành Công ", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub
    Private Sub xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xoa.Click
        Dim x As TreeNode
        x = Tr_ToChuc.SelectedNode
        If IsNothing(x) = True Then
            MessageBox.Show("Không có đối tượng xoá", "Thông Báo", MessageBoxButtons.OK)
        Else
            Dim m As String
            m = Tr_ToChuc.SelectedNode.FirstNode.Text
            'Tr_ToChuc.SelectedNode = Nothing           
            For Each dr As DataRow In data.Rows
                If dr("MaNV") = Lv_DSNV.Items(vt).SubItems(1).Text Then 'm
                    Lv_DSNV.Items.RemoveAt(vt)
                    For j As Integer = 0 To Tr_ToChuc.SelectedNode.Nodes.Count - 1
                        If x.Nodes.Item(j).FirstNode.Text = dr("MaNV") Then
                            x.Nodes.Item(j).Remove()
                            Exit For
                        End If
                    Next
                    dr.Delete()
                    Refesh_()
                    Exit For
                End If
            Next
        End If
    End Sub

    Private Sub capnhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles capnhat.Click
        If status = True Then
            Dim x As TreeNode
            Dim m As String
            xlnv = New XLNhanVien
            x = Tr_ToChuc.SelectedNode 'Lay node da chon
            m = Tr_ToChuc.SelectedNode.FirstNode.Text ' lay node con ke node da chon   lay manv  
            Dim nv As New DTO_NhanVienCT.NhanVien
            If LayDLieu(nv, 0) = False Then
                For Each dr As DataRow In data.Rows
                    If dr("MaNV") = Lv_DSNV.Items(vt).SubItems(1).Text Then 'dlieu' 
                        xlnv.BUS_Add_Up_NV(dr, nv) 'thuc hien cap nhat vao datatable
                        'cap nhat len treeview
                        For j As Integer = 0 To x.Nodes.Count - 1
                            If x.Nodes.Item(j).Nodes.Item(0).Text = dlieu Then
                                x.Nodes.Item(j).Text = nv.HoTen
                                x.Nodes.Item(j).Nodes.Item(0).Text = nv.MaNV
                                x.Nodes.Item(j).Nodes.Item(1).Text = nv.NgaySinh
                                x.Nodes.Item(j).Nodes.Item(2).Text = nv.GioiTinh
                                x.Nodes.Item(j).Nodes.Item(3).Text = nv.NoiSinh
                                x.Nodes.Item(j).Nodes.Item(4).Text = nv.NguyenQuan
                                x.Nodes.Item(j).Nodes.Item(5).Text = nv.CMND
                                x.Nodes.Item(j).Nodes.Item(6).Text = nv.NgayCap
                                x.Nodes.Item(j).Nodes.Item(7).Text = nv.NoiCap
                                x.Nodes.Item(j).Nodes.Item(8).Text = nv.DienThoai
                                x.Nodes.Item(j).Nodes.Item(9).Text = nv.DiaChi
                                x.Nodes.Item(j).Nodes.Item(10).Text = nv.DanToc
                                x.Nodes.Item(j).Nodes.Item(11).Text = nv.TonGiao
                                x.Nodes.Item(j).Nodes.Item(12).Text = nv.HocVan
                                x.Nodes.Item(j).Nodes.Item(13).Text = nv.BangCap
                                x.Nodes.Item(j).Nodes.Item(14).Text = nv.ChucVu
                                x.Nodes.Item(j).Nodes.Item(15).Text = nv.QuocTich
                                x.Nodes.Item(j).Nodes.Item(16).Text = nv.NgoaiNgu
                                x.Nodes.Item(j).Nodes.Item(17).Text = nv.VaoDoan
                                x.Nodes.Item(j).Nodes.Item(18).Text = nv.VaoDang
                                x.Nodes.Item(j).Nodes.Item(19).Text = nv.QTBanThan
                                x.Nodes.Item(j).Nodes.Item(20).Text = maph
                                x.Nodes.Item(j).Nodes.Item(21).Text = filePatch
                            End If
                        Next
                        ' cap nhat lai trên listview
                        Lv_DSNV.Items(vt).SubItems(1).Text = nv.MaNV
                        Lv_DSNV.Items(vt).SubItems(2).Text = nv.HoTen
                        Lv_DSNV.Items(vt).SubItems(3).Text = nv.NgaySinh
                        Lv_DSNV.Items(vt).SubItems(4).Text = nv.GioiTinh
                        Lv_DSNV.Items(vt).SubItems(5).Text = nv.NoiSinh
                        Lv_DSNV.Items(vt).SubItems(6).Text = nv.NguyenQuan
                        Lv_DSNV.Items(vt).SubItems(7).Text = nv.CMND
                        Lv_DSNV.Items(vt).SubItems(8).Text = nv.NgayCap
                        Lv_DSNV.Items(vt).SubItems(9).Text = nv.NoiCap
                        Lv_DSNV.Items(vt).SubItems(10).Text = nv.DienThoai
                        Lv_DSNV.Items(vt).SubItems(11).Text = nv.DiaChi
                        Lv_DSNV.Items(vt).SubItems(12).Text = nv.DanToc
                        Lv_DSNV.Items(vt).SubItems(13).Text = nv.TonGiao
                        Lv_DSNV.Items(vt).SubItems(14).Text = nv.HocVan
                        Lv_DSNV.Items(vt).SubItems(15).Text = nv.BangCap
                        Lv_DSNV.Items(vt).SubItems(16).Text = nv.ChucVu
                        Lv_DSNV.Items(vt).SubItems(17).Text = nv.QuocTich
                        Lv_DSNV.Items(vt).SubItems(18).Text = nv.NgoaiNgu
                        Lv_DSNV.Items(vt).SubItems(19).Text = nv.VaoDoan
                        Lv_DSNV.Items(vt).SubItems(20).Text = nv.VaoDang
                        Lv_DSNV.Items(vt).SubItems(21).Text = nv.QTBanThan
                        Lv_DSNV.Items(vt).SubItems(22).Text = maph
                        Lv_DSNV.Items(vt).SubItems(23).Text = filePatch
                    End If
                Next
            End If
            'Tr_ToChuc.SelectedNode = Nothing
            Refesh_()
        Else
            MessageBox.Show("Chưa Chọn Đối Tượng Cần Cập Nhật Trong ListView", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub
    Private Sub them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles them.Click
        ThemNode()
        Refesh_()
    End Sub

    Private Sub close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles close_.Click
        Me.Close()
    End Sub
    Private Sub Lamlai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lamlai.Click
        Refesh_()
    End Sub

    Private Sub tb_duyethinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb_duyethinh.Click
        DuyetAnh()
    End Sub

    Private Sub reload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reload.Click
        Tr_ToChuc.Nodes.Clear()
        RLoad()
        Refesh_()
    End Sub

    Private Sub Lv_DSNV_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lv_DSNV.SelectedIndexChanged
        Try
            vt = Lv_DSNV.SelectedItems(0).Index
            MaNV.Text = Lv_DSNV.SelectedItems(0).SubItems(1).Text()
            dlieu = Lv_DSNV.SelectedItems(0).SubItems(1).Text()
            tb_hoten.Text = Lv_DSNV.SelectedItems(0).SubItems(2).Text()
            dt_ngaysinh.Text = Lv_DSNV.SelectedItems(0).SubItems(3).Text()
            If Lv_DSNV.SelectedItems(0).SubItems(4).Text() = "Nam" Then
                rd_nam.Checked = True
            Else
                rd_nu.Checked = True
            End If
            tb_noisinh.Text = Lv_DSNV.SelectedItems(0).SubItems(5).Text()
            tb_nguyenquan.Text = Lv_DSNV.SelectedItems(0).SubItems(6).Text()
            tb_cmnd.tb_nhapso.Text = Lv_DSNV.SelectedItems(0).SubItems(7).Text()
            dt_ngaycap.Text = Lv_DSNV.SelectedItems(0).SubItems(8).Text()
            tb_noicap.Text = Lv_DSNV.SelectedItems(0).SubItems(9).Text()
            tb_dienthoai.tb_nhapso.Text = Lv_DSNV.SelectedItems(0).SubItems(10).Text()
            tb_diachi.Text = Lv_DSNV.SelectedItems(0).SubItems(11).Text()
            cb_dantoc.Text = Lv_DSNV.SelectedItems(0).SubItems(12).Text()
            cb_tongiao.Text = Lv_DSNV.SelectedItems(0).SubItems(13).Text()
            tb_hocvan.Text = Lv_DSNV.SelectedItems(0).SubItems(14).Text()
            cb_bangcap.Text = Lv_DSNV.SelectedItems(0).SubItems(15).Text()
            cb_chucvu.Text = Lv_DSNV.SelectedItems(0).SubItems(16).Text()
            cb_qtich.Text = Lv_DSNV.SelectedItems(0).SubItems(17).Text()
            tb_ngoaingu.Text = Lv_DSNV.SelectedItems(0).SubItems(18).Text()
            tb_vaodoan.Text = Lv_DSNV.SelectedItems(0).SubItems(19).Text()
            tb_vaodang.Text = Lv_DSNV.SelectedItems(0).SubItems(20).Text()
            tb_quatrinh.Text = Lv_DSNV.SelectedItems(0).SubItems(21).Text()
            filePatch = Lv_DSNV.SelectedItems(0).SubItems(23).Text()
            If MyImage IsNot Nothing Then
                MyImage.Dispose()
            End If
            If filePatch <> "" Then
                pic_nv.SizeMode = PictureBoxSizeMode.StretchImage
                MyImage = New Bitmap(".\" & filePatch)
                pic_nv.ClientSize = New Size(pic_nv.Size)
                pic_nv.Image = CType(MyImage, Image)
            End If
            status = True
        Catch ex As Exception
        End Try
    End Sub
    Private Sub NhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RLoad()
    End Sub

    Private Sub NhanVien_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim dr As DialogResult = MessageBox.Show("Bạn có muốn lưu dữ liệu lại trước khi đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dr = Windows.Forms.DialogResult.Yes Then
            xlnv = New XLNhanVien
            If xlnv.BUS_SaveNV(data) = True Then
                MessageBox.Show("Lưu Thành Công ", "Thông Báo", MessageBoxButtons.OK)
            End If
        End If
    End Sub

    Private Sub Tr_ToChuc_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles Tr_ToChuc.AfterSelect
        xlnv = New XLNhanVien
        nodeClick = Tr_ToChuc.SelectedNode.Text
        Dim i As Integer = 0
        If nodeClick <> "CTy TNHH Cộng Sự Giải Pháp Thông Tin" Then
            dsnv = xlnv.BUS_TimNhanVien(nodeClick, maph, data, dspb)
            If dsnv.Count <> 0 Then
                Lv_DSNV.Items.Clear()
                Lv_DSNV.View = View.Details
                Lv_DSNV.GridLines = True
                Lv_DSNV.FullRowSelect = True
                For Each nv As DTO_NhanVienCT.NhanVien In dsnv
                    LviewShow(nv)
                Next
            End If
        End If
        Refesh_()
    End Sub


#Region "Thuc hien"
    Private Sub RLoad()
        Dim tg As New XLTonGiao
        Dim dt As New XLDanToc
        Dim bc As New XLBangCap
        Dim pb As New XLPhongBan
        Dim cv As New XLChucVu
        Dim qt As New XLQuocTich
        xlnv = New XLNhanVien
        data = New DataTable
        dsbc = New ArrayList
        dsdt = New ArrayList
        dstg = New ArrayList
        dsqt = New ArrayList
        dscv = New ArrayList
        dspb = New List(Of DTO_PhongBan.PhongBan)
        Dim tree_ As New TreeNode("CTy TNHH Cộng Sự Giải Pháp Thông Tin")
        rd_nam.Checked = True
        dstg = tg.BUS_ArrayTG()
        dsbc = bc.BUS_ArrayBC()
        dsdt = dt.BUS_ArrayDT()
        dspb = pb.BUS_LayDSPB()
        dscv = cv.BUS_ArrayCV()
        dsqt = qt.BUS_ArrayQT()
        data = xlnv.BUS_LayDSNV()
        lbMaNV()
        If data.Rows.Count = 0 Then Return
        If dspb.Count < 0 Then
        Else
            For Each d As DTO_PhongBan.PhongBan In dspb
                tree_.Nodes.Add(xlnv.BUS_AddTreePB(d.TenPhong, d.MaPB, data))
            Next
            Tr_ToChuc.Nodes.Add(tree_)
            Tr_ToChuc.ExpandAll()
        End If
        XuatComBo()
    End Sub
    Private Sub XuatComBo()
        cb_bangcap.DataSource = dsbc
        cb_dantoc.DataSource = dsdt
        cb_tongiao.DataSource = dstg
        cb_chucvu.DataSource = dscv
        cb_qtich.DataSource = dsqt
        cb_bangcap.DisplayMember = "TenBC"
        cb_dantoc.DisplayMember = "TenDanToc"
        cb_tongiao.DisplayMember = "TenTonGiao"
        cb_chucvu.DisplayMember = "TenChucVu"
        cb_qtich.DisplayMember = "TenNuoc"
    End Sub
    Private Sub DuyetAnh()
        Dim myStream As Stream = Nothing
        Dim openFileDialog1 As New OpenFileDialog
        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All file(*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True
        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Try
                myStream = openFileDialog1.OpenFile()
                If myStream IsNot Nothing Then
                    pic_nv.SizeMode = PictureBoxSizeMode.StretchImage
                    MyImage = New Bitmap(openFileDialog1.FileName)
                    pic_nv.ClientSize = New Size(pic_nv.Size)
                    pic_nv.Image = CType(MyImage, Image)
                    'thuc hien luu file
                    Dim n As String = openFileDialog1.SafeFileName 'lay name file và dinh dang duoi
                    Dim directoryPath As String = "StoredFiles"
                    If Directory.Exists(directoryPath) <> True Then 'kiem tra khoi tao foler chua file anh
                        Directory.CreateDirectory(directoryPath)
                    End If
                    filePatch = directoryPath & "\" & n 'tao duong dan den file vd: StoredFiles\Tai nang.JPG
                    Dim fs As FileStream = New FileStream(filePatch, FileMode.Create) 'khoi tao file trong duong dan da tao
                    'neu lam toi day thi file khoi tao khong ca du lieu. chi co ten file
                    Dim b As Char = n.Substring(n.LastIndexOf(".")) 'lay chuoi dinh dang file
                    Select Case b
                        Case ".Jpg"
                            Me.pic_nv.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
                        Case ".Gif"
                            Me.pic_nv.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Gif)
                        Case ".Bmp"
                            Me.pic_nv.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp)
                    End Select
                End If
            Catch ex As Exception
                MessageBox.Show("Cannot read file from disk. Original error" & ex.Message)
            Finally
                If myStream IsNot Nothing Then
                    myStream.Close()
                End If
            End Try
        End If
    End Sub

    Private Sub lbMaNV()
        If data.Rows.Count = 0 Then
            Dim x As New XLThamSo
            MaNV.Text = x.LayMANV()
        Else
            MaNV.Text = xlnv.BUS_SLManv(data.Rows(data.Rows.Count - 1).Item(0).ToString()) 'lay ma nhan vien
        End If
    End Sub
    Private Sub ThemNode()
        xlnv = New XLNhanVien
        Dim nv As New DTO_NhanVienCT.NhanVien
        Dim x As TreeNode
        x = Tr_ToChuc.SelectedNode
        If IsNothing(x) = True Then
            MessageBox.Show("Bạn Chưa chọn vị trí phòng cần thêm ", "Thông Báo", MessageBoxButtons.OK)
        Else
            lbMaNV()
            If LayDLieu(nv, 1) = False Then '1 cho biet la them dlieu
                If xlnv.BUS_TimMaPB(nodeClick, dspb) <> "" Then 'Tim Ma phong ban
                    'Dim nv As New DTO_NhanVienCT.NhanVien
                    Dim dt As DataRow = data.NewRow()
                    'xlnv = New XLNhanVien
                    xlnv.BUS_Add_Up_NV(dt, nv) 'them nv vào dt
                    data.Rows.Add(dt) 'them vao data
                    LviewShow(nv) 'hien danh sach len listview
                    'hien len treeview
                    x.Nodes.Add(xlnv.BUS_AddTreeNV(nv))
                End If
                'Tr_ToChuc.SelectedNode = Nothing
            Else
                MessageBox.Show("Dữ liệu nhập vào sai hoặc thiếu", "Thông Báo", MessageBoxButtons.OK)
            End If
        End If
    End Sub

    Private Function LayDLieu(ByRef nv As DTO_NhanVienCT.NhanVien, ByVal dau As Integer) As Boolean
        xlnv = New XLNhanVien
        nv.MaNV = MaNV.Text
        nv.HoTen = tb_hoten.Text
        nv.NgaySinh = dt_ngaysinh.Text
        nv.GioiTinh = "Nam"
        If rd_nu.Checked = True Then
            nv.GioiTinh = "Nu"
        End If
        nv.NoiSinh = tb_noisinh.Text
        nv.NguyenQuan = tb_nguyenquan.Text
        nv.CMND = 0
        If tb_cmnd.tb_nhapso.Text <> "" Then
            nv.CMND = tb_cmnd.tb_nhapso.Text
        End If
        nv.NgayCap = dt_ngaycap.Text
        nv.NoiCap = tb_noicap.Text
        nv.DienThoai = 0
        If tb_dienthoai.tb_nhapso.Text <> "" Then
            nv.DienThoai = tb_dienthoai.tb_nhapso.Text
        End If
        nv.DiaChi = tb_diachi.Text
        nv.DanToc = cb_dantoc.Text
        nv.TonGiao = cb_tongiao.Text
        nv.HocVan = tb_hocvan.Text
        nv.BangCap = cb_bangcap.Text
        nv.ChucVu = cb_chucvu.Text
        nv.QuocTich = cb_qtich.Text
        nv.NgoaiNgu = tb_ngoaingu.Text
        nv.VaoDoan = tb_vaodoan.Text
        If tb_vaodoan.Text = "" Then
            nv.VaoDoan = "Khong"
        End If
        nv.VaoDang = tb_vaodang.Text
        If tb_vaodang.Text = "" Then
            nv.VaoDang = "Khong"
        End If
        nv.QTBanThan = tb_quatrinh.Text
        nv.BoPhan = maph
        nv.Hinh = filePatch
        Return xlnv.BUS_KtraThem(nv, data, dau)
    End Function

    Private Sub LviewShow(ByVal nv As DTO_NhanVienCT.NhanVien)
        Dim item As New ListViewItem
        item.Text = "STT"
        item.SubItems.Add(nv.MaNV)
        item.SubItems.Add(nv.HoTen)
        item.SubItems.Add(nv.NgaySinh)
        item.SubItems.Add(nv.GioiTinh)
        item.SubItems.Add(nv.NoiSinh)
        item.SubItems.Add(nv.NguyenQuan)
        item.SubItems.Add(nv.CMND)
        item.SubItems.Add(nv.NgayCap)
        item.SubItems.Add(nv.NoiCap)
        item.SubItems.Add(nv.DienThoai)
        item.SubItems.Add(nv.DiaChi)
        item.SubItems.Add(nv.DanToc)
        item.SubItems.Add(nv.TonGiao)
        item.SubItems.Add(nv.HocVan)
        item.SubItems.Add(nv.BangCap)
        item.SubItems.Add(nv.ChucVu)
        item.SubItems.Add(nv.QuocTich)
        item.SubItems.Add(nv.NgoaiNgu)
        item.SubItems.Add(nv.VaoDoan)
        item.SubItems.Add(nv.VaoDang)
        item.SubItems.Add(nv.QTBanThan)
        item.SubItems.Add(nv.BoPhan)
        item.SubItems.Add(nv.Hinh)
        Lv_DSNV.Items.Add(item)
        capnhatSTT()
    End Sub
    
    Private Sub capnhatSTT()
        For i As Integer = 1 To Lv_DSNV.Items.Count
            Lv_DSNV.Items(i - 1).Text = i.ToString
        Next
    End Sub
    Private Sub Refesh_()
        pic_nv.Image = Nothing
        tb_hoten.Text = String.Empty
        dt_ngaysinh.Value = DateTime.Now
        rd_nam.Checked = True
        tb_noisinh.Text = String.Empty
        tb_nguyenquan.Text = String.Empty
        tb_cmnd.tb_nhapso.Text = String.Empty
        dt_ngaycap.Text = DateTime.Now
        tb_noicap.Text = String.Empty
        tb_dienthoai.tb_nhapso.Text = String.Empty
        tb_diachi.Text = String.Empty
        cb_dantoc.SelectedIndex = 0
        cb_tongiao.SelectedIndex = 0
        tb_hocvan.Text = String.Empty
        cb_bangcap.SelectedIndex = 0
        cb_chucvu.SelectedIndex = 0
        cb_qtich.SelectedIndex = 0
        tb_ngoaingu.Text = String.Empty
        tb_vaodoan.Text = String.Empty
        tb_vaodang.Text = String.Empty
        tb_quatrinh.Text = String.Empty
        filePatch = String.Empty
        status = False
    End Sub
#End Region

End Class

